home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Buttons.C < prev    next >
C/C++ Source or Header  |  1992-06-10  |  8KB  |  354 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "Buttons.h"
  6.  
  7. #include "Class.h"
  8. #include "TextItem.h"
  9. #include "WindowSystem.h"
  10. #include "Look.h"
  11.  
  12. bool gInButtonFlush;
  13.  
  14. //---- Button ------------------------------------------------------------------
  15.  
  16. NewAbstractMetaImpl0(Button,CompositeVObject);
  17.  
  18. Button::Button(int id, VObject *vop) : CompositeVObject(id, vop, 0)
  19. {
  20. }
  21.  
  22. Command *Button::DoLeftButtonDownCommand(Point, Token, int)
  23. {
  24.     return new ButtonCommand(this, TestFlag(eButtonIdle));
  25. }
  26.  
  27. void Button::Flush(int msec)
  28. {
  29.     if (Enabled()) {
  30.     gInButtonFlush= TRUE;
  31.     DoTrackMouse(eTrackPress, contentRect.Center());
  32.     GraphicDelay(msec);
  33.     DoTrackMouse(eTrackRelease, contentRect.Center());
  34.     gInButtonFlush= FALSE;
  35.     }
  36. }
  37.  
  38. void Button::DoTrackMouse(TrackPhase atp, Point lp)
  39. {
  40.     if (Enabled() && TestFlag(eButtonIdle)) {
  41.     switch (atp) {
  42.     case eTrackPress:
  43.         Highlight(On);
  44.     case eTrackMove:
  45.     case eTrackIdle:
  46.         Control(GetId(), cPartToggle, (void*) 1);
  47.         break;
  48.     case eTrackRelease:
  49.     case eTrackExit:
  50.         Highlight(Off);
  51.         break;
  52.     default:
  53.         break;
  54.     }
  55.     } else
  56.     CompositeVObject::DoTrackMouse(atp, lp);
  57. }
  58.  
  59. void Button::DrawHighlight(Rectangle)
  60. {
  61. }
  62.  
  63. void Button::SetOrigin(Point at)
  64. {
  65.     VObject::SetOrigin(at);
  66.     if (Size() > 0)
  67.     At(0)->Align(at, GetExtent(), (VObjAlign)(eVObjHCenter|eVObjVCenter));   
  68. }
  69.  
  70. void Button::SetLabel(char *lb, bool redraw)
  71. {
  72.     if (At(0)->IsKindOf(TextItem)) {
  73.     TextItem *ti= (TextItem*) At(0);
  74.     ti->SetString(lb);
  75.     SetOrigin(GetOrigin());
  76.     if (redraw)
  77.         ForceRedraw();
  78.     }
  79. }
  80.  
  81. Metric Button::GetMinSize()
  82. {
  83.     return CompositeVObject::GetMinSize();
  84. }
  85.  
  86. //---- ButtonCommand -----------------------------------------------------------
  87.  
  88. ButtonCommand::ButtonCommand(Button* bf, bool idl)
  89. {
  90.     item= bf;
  91.     lastinside= FALSE;
  92.     SetFlag(eCmdIdleEvents, idl);
  93. }
  94.  
  95. void ButtonCommand::TrackFeedback(Point, Point, bool)
  96. {
  97. }
  98.  
  99. Command *ButtonCommand::TrackMouse(TrackPhase atp, Point, Point, Point np)
  100. {
  101.     inside= item->ContainsPoint(np);
  102.     switch(atp) {
  103.     case eTrackPress:
  104.     if (inside)
  105.         item->DoTrackMouse(eTrackPress, np);
  106.     break;        
  107.     case eTrackIdle:
  108.     if (inside)
  109.         item->DoTrackMouse(eTrackIdle, np);
  110.     break;
  111.     case eTrackExit:
  112.     item->DoTrackMouse(eTrackExit, np);
  113.     break;
  114.     case eTrackRelease:
  115.     if (inside)
  116.         item->DoTrackMouse(eTrackRelease, np);
  117.     else
  118.         item->DoTrackMouse(eTrackExit, np);
  119.     return gNoChanges;
  120.     case eTrackMove:
  121.     if (lastinside && !inside)
  122.         item->DoTrackMouse(eTrackExit, np);
  123.     else if (!lastinside && inside)
  124.         item->DoTrackMouse(eTrackPress, np);
  125.     else if (inside)
  126.         item->DoTrackMouse(eTrackMove, np);
  127.     break;
  128.     }
  129.     lastinside= inside;
  130.     return this;
  131. }
  132.  
  133. //---- ActionButton ------------------------------------------------------------
  134.  
  135. NewMetaImpl0(ActionButton,Button);
  136.  
  137. ActionButton::ActionButton(int id, VObject *g, bool dflt) : Button(id, g)
  138. {
  139.     SetFlag(eActionDefaultButton, dflt);
  140.     SetFlag(eVObjVFixed);
  141. }
  142.  
  143. ActionButton::ActionButton(int id, char *t, bool dflt) : Button(id, new TextItem(t))
  144. {
  145.     SetFlag(eActionDefaultButton, dflt);
  146.     SetFlag(eVObjVFixed);
  147. }
  148.  
  149. Metric ActionButton::GetMinSize()
  150. {
  151.     if (TestFlag(eActionDefaultButton))
  152.     return gLook->DefaultButtonLayout()->GetMinSize(this);
  153.     return gLook->ActionButtonLayout()->GetMinSize(this);
  154. }
  155.  
  156. void ActionButton::DrawInner(Rectangle, bool highlight)
  157. {
  158.     int code= 0;
  159.     if (Enabled())
  160.     SETBIT(code, 2);
  161.     if (highlight)
  162.     SETBIT(code, 3);
  163.     if (TestFlag(eActionDefaultButton))
  164.     gLook->DefaultButtonLayout()->Adorn(this, contentRect, code);
  165.     else
  166.     gLook->ActionButtonLayout()->Adorn(this, contentRect, code);
  167. }
  168.  
  169. void ActionButton::Control(int id, int part, void *val)
  170. {
  171.     if (part == cPartToggle)
  172.     part= cPartAction;
  173.     Button::Control(id, part, val);
  174. }
  175.  
  176. //---- StateButton -------------------------------------------------------------
  177.  
  178. NewAbstractMetaImpl(StateButton,Button, (T(state)));
  179.  
  180. StateButton::StateButton(int id, int s) : Button(id)
  181. {
  182.     state= s;
  183. }
  184.  
  185. StateButton::StateButton(int id, char *label, int s) : Button(id)
  186. {
  187.     state= s;
  188.     if (label)
  189.     Add(new TextItem(label));
  190. }
  191.  
  192. StateButton::StateButton(int id, VObject *label, int s) : Button(id)
  193. {
  194.     state= s;
  195.     if (label)
  196.     Add(label);
  197. }
  198.  
  199. void StateButton::DoTrackMouse(TrackPhase atp, Point pp)
  200. {
  201.     if (Enabled() && atp == eTrackRelease) {
  202.     SetValue((int) !state);
  203.     Control(GetId(), cPartToggle, (void*) state);
  204.     Highlight(Off);
  205.     } else
  206.     Button::DoTrackMouse(atp, pp);
  207. }
  208.  
  209. void StateButton::SetValue(int st, bool redraw)
  210. {
  211.     if (st != state) {
  212.     state= st;
  213.     Changed();
  214.     if (redraw)
  215.         ForceRedraw();
  216.     }
  217. }
  218.  
  219. int StateButton::GetValue()
  220. {
  221.     return state;
  222. }
  223.  
  224. OStream& StateButton::PrintOn(OStream &s)
  225. {
  226.     Button::PrintOn(s);
  227.     return s << state SP;
  228. }
  229.  
  230. IStream& StateButton::ReadFrom(IStream &s)
  231. {
  232.     Button::ReadFrom(s);
  233.     return s >> state;
  234. }
  235.  
  236. //---- VObjectButton -----------------------------------------------------------
  237.  
  238. NewMetaImpl0(VObjectButton,StateButton);
  239.  
  240. VObjectButton::VObjectButton(int id, VObject *vop, int st) : StateButton(id, st)
  241. {
  242.     Add(vop);
  243. }
  244.  
  245. Metric VObjectButton::GetMinSize()
  246. {
  247.     VObject *inner= At(0);
  248.     Metric m= gLook->ActionButtonLayout()->GetMinSize(this);
  249.     SetFlag(eVObjHFixed, inner->TestFlag(eVObjHFixed));
  250.     SetFlag(eVObjVFixed, inner->TestFlag(eVObjVFixed));
  251.     return m;
  252. }
  253.  
  254. void VObjectButton::DrawInner(Rectangle, bool highlight)
  255. {
  256.     int code= 0;
  257.     if (state)
  258.     SETBIT(code, 1);
  259.     if (Enabled())
  260.     SETBIT(code, 2);
  261.     if (highlight)
  262.     SETBIT(code, 3);
  263.     gLook->ActionButtonLayout()->Adorn(this, contentRect, code);
  264. }
  265.  
  266. //---- RadioButton -------------------------------------------------------------
  267.  
  268. NewMetaImpl0(RadioButton,StateButton);
  269.  
  270. RadioButton::RadioButton(int id, bool state) : StateButton(id, state)
  271. {
  272.     SetFlag(eVObjVFixed|eVObjHFixed);
  273. }
  274.  
  275. RadioButton::RadioButton(int id, char *label, bool state)
  276.                         : StateButton(id, label, state)
  277. {
  278.     SetFlag(eVObjVFixed|eVObjHFixed);
  279. }
  280.  
  281. RadioButton::RadioButton(int id, VObject *label, bool state)
  282.                         : StateButton(id, label, state)
  283. {
  284.     SetFlag(eVObjVFixed|eVObjHFixed);
  285. }
  286.  
  287. Metric RadioButton::GetMinSize()
  288. {
  289.     return gLook->RadioButtonLayout()->GetMinSize(this);
  290. }
  291.  
  292. void RadioButton::SetOrigin(Point at)
  293. {
  294.     VObject::SetOrigin(at);
  295.     gLook->RadioButtonLayout()->SetOrigin(this, at);
  296. }
  297.  
  298. void RadioButton::DrawInner(Rectangle, bool highlight)
  299. {
  300.     int code= 0;
  301.     if (state)
  302.     SETBIT(code, 1);
  303.     if (Enabled())
  304.     SETBIT(code, 2);
  305.     if (highlight)
  306.     SETBIT(code, 3);
  307.     gLook->RadioButtonLayout()->Adorn(this, contentRect, code);
  308. }
  309.  
  310. //---- ToggleButton ------------------------------------------------------------
  311.  
  312. NewMetaImpl0(ToggleButton,StateButton);
  313.  
  314. ToggleButton::ToggleButton(int id, bool state) : StateButton(id, state)
  315. {
  316.     SetFlag(eVObjVFixed|eVObjHFixed);
  317. }
  318.  
  319. ToggleButton::ToggleButton(int id, char *label, bool state)
  320.                         : StateButton(id, label, state)
  321. {
  322.     SetFlag(eVObjVFixed|eVObjHFixed);
  323. }
  324.  
  325. ToggleButton::ToggleButton(int id, VObject *label, bool state)
  326.                         : StateButton(id, label, state)
  327. {
  328.     SetFlag(eVObjVFixed|eVObjHFixed);
  329. }
  330.  
  331. Metric ToggleButton::GetMinSize()
  332. {
  333.     return gLook->ToggleButtonLayout()->GetMinSize(this);
  334. }
  335.  
  336. void ToggleButton::SetOrigin(Point at)
  337. {
  338.     VObject::SetOrigin(at);
  339.     gLook->ToggleButtonLayout()->SetOrigin(this, at);
  340. }
  341.  
  342. void ToggleButton::DrawInner(Rectangle, bool highlight)
  343. {
  344.     int code= 0;
  345.     if (state)
  346.     SETBIT(code, 1);
  347.     if (Enabled())
  348.     SETBIT(code, 2);
  349.     if (highlight)
  350.     SETBIT(code, 3);
  351.     gLook->ToggleButtonLayout()->Adorn(this, contentRect, code);
  352. }
  353.  
  354.